package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("借阅图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要借阅图书的名字！");
        String name = scanner.nextLine();
        int currentSize = bookList.getUseSize();
        int pos = -1;
        //查看需要借阅的书有没有
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if(pos == -1) {
            System.out.println("没有找到你所要借阅的图书!");
            return;
        }
        Book book = bookList.getBook(pos);
        if(book.getIsBorrowed()){
            System.out.println("书已借出！");
            return;
        }
        book.setIsBorrowed(true);
        System.out.println("借阅成功");
        System.out.println(book);
    }
}
